﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CommonLib.Pattern;
public class EPChainNodeEscapeOther : EPChainNodeNone
{
	protected override bool TryDeal(object o)
	{
		RoleCtrl role = o as RoleCtrl;
		/*
		if(IsDangrous(role.gameObject, role.transform.position, new Vector2(15,15), out Vector2 fleeDir))
		{

			role.SubManagerMove.SetVelocityDir((fleeDir - role.SubManagerMove.VelocityDir).normalized);
			Debug.Log("EPChainNodeEscapeOther :" + fleeDir);
			return true;
		}
		*/
		if(FindOthers(role, role.transform.position, new Vector2(15, 15), out Collider2D col))
		{
			Vector2 fleer = new Vector2(role.transform.position.x - col.transform.position.x,
				role.transform.position.y - col.transform.position.y).normalized;

			Vector2 desired = (role.SubManagerMove.VelocityDir + fleer) / 2;
			//role.SubManagerMove.Rb2d.velocity = desired.normalized * role.RoleEntity.CurrentSpeed;
			role.SubManagerMove.SetVelocityDir(desired);
			Debug.Log("EPChainNodeEscapeOther");
			return true;
		}
		return false;
	}


	protected virtual bool IsDangrous(UnityEngine.GameObject @object, Vector2 center, Vector2 size, out Vector2 fleeDir)
	{
		fleeDir = Vector2.zero;
		float xDir = 0;
		float yDir = 0;
		Collider2D[] collider2D = Physics2D.OverlapBoxAll(center, size, 0);
		if (collider2D.Length <= 1)
			return false;
		else
		{
			bool hasDangrous = false;
			int count = 0;
			for (int i = 0; i < collider2D.Length; i++)
			{
				if (collider2D[i].gameObject.tag == "Player" && collider2D[i].gameObject != @object)
				{
					RoleCtrl temp = collider2D[i].GetComponent<RoleCtrl>();
					Vector2 dir = (@object.transform.position - collider2D[i].transform.position).normalized;
					float distance = Vector3.Distance(collider2D[i].transform.position , @object.transform.position);
					float weight = 1 - distance / 15.0f;
					xDir += weight * dir.x;
					yDir += weight * dir.y;
					count++;
					//是否有危险目标
					hasDangrous = hasDangrous||true;
				}
			}
			if (hasDangrous)
			{
				Debug.Log("EPChainNodeEscapeOther :" + $"{xDir} + : {yDir} =》 {count}");
				fleeDir = new Vector2(xDir / (count * 1.0f) , yDir / (count * 1.0f));
				return true;
			}
		}
		// Physics.OverlapBox(transform.position, size, Quaternion.identity)
		return false;
	}


	protected virtual bool FindOthers(RoleCtrl @object, Vector2 center, Vector2 size, out Collider2D collider)
	{
		collider = null;
		float length = 999;
		Collider2D[] collider2D = Physics2D.OverlapBoxAll(center, size, 0);
		if (collider2D.Length <= 1)
			return false;
		else
		{

			for (int i = 0; i < collider2D.Length; i++)
			{
				if (collider2D[i].gameObject.tag == "Player" && collider2D[i].gameObject != @object.gameObject)
				{
					if (@object.RoleEntity.CurrentScore < collider2D[i].gameObject.GetComponent<RoleCtrl>().RoleEntity.CurrentScore)
					{
						if (Vector3.Distance(@object.transform.position, collider2D[i].transform.position) < length)
						{
							collider = collider2D[i];
						}
					}
					//return true;
				}
			}
			if (null != collider) return true;
		}
		// Physics.OverlapBox(transform.position, size, Quaternion.identity)
		return false;
	}
}
